package com.eleadmin.store.service.impl;

import com.eleadmin.damon.utils.results.PageResult2;
import com.eleadmin.damon.utils.utils.ArithUtil;
import com.eleadmin.damon.utils.utils.Utils;
import com.eleadmin.common.core.web.JsonResult;
import com.eleadmin.common.system.entity.User;
//import com.eleadmin.store.mapper.EntryBillSonMapper;
import com.eleadmin.store.biz.EntryBillBiz;
import com.eleadmin.store.biz.ProductStorageBiz;
import com.eleadmin.store.entity.EntryBill;
import com.eleadmin.store.entity.EntryBillSon;
import com.eleadmin.store.entity.ProductStorage;
import com.eleadmin.store.service.EntryBillSonService;
import org.springframework.stereotype.Service;
import com.eleadmin.store.biz.EntryBillSonBiz;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
//import javax.transaction.Transactional;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 入库单子表服务实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2024-02-14 15:00:12
 */
@Service
public class EntryBillSonServiceImpl /*extends ServiceImpl<EntryBillSonMapper, EntryBillSon> */ implements EntryBillSonService {

    @Autowired
    private EntryBillSonBiz entryBillSonBiz;
    @Autowired
    private EntryBillBiz entryBillBiz;
    @Autowired
    private ProductStorageBiz productStorageBiz;

    @Override
    public JsonResult list(PageResult2<T> page, EntryBillSon entryBillSon, User loginUser) throws Exception {
        return entryBillSonBiz.list(page, entryBillSon, loginUser);
    }

    @Override
    public JsonResult list(EntryBillSon entryBillSon, User loginUser) {
        return JsonResult.ok().setData(entryBillSonBiz.list(entryBillSon, loginUser));
    }

    @Override
    public JsonResult calcTotalPrice(EntryBillSon entryBillSon, User loginUser) {
        return JsonResult.ok().setData(entryBillSonBiz.calcTotalPrice(entryBillSon, loginUser));
    }

    @Override
    public JsonResult getById(String id) {
        return JsonResult.ok().setData(entryBillSonBiz.getById(id));
    }

    @Transactional
    @Override
    public JsonResult save(EntryBillSon entryBillSon, User loginUser) {
        if (entryBillSonBiz.save(entryBillSon, loginUser)) {
            //主表 总金额更新
            Double total = entryBillSonBiz.calcTotalPrice(entryBillSon, loginUser);
            EntryBill entryBill = new EntryBill(entryBillSon.getEntryBillId() + "", total + "");
            entryBillBiz.updateTotal(entryBill, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(entryBillSon.getWarehousename());
            ps.setProductname(entryBillSon.getProductname());
            //保存的时候有就可以了  下面哪个修改的方法code就不用加了
            ps.setCode(entryBillSon.getCode());
            ps.setSpecification(entryBillSon.getSpecification());
            ps.setLengths(entryBillSon.getLengths() + "");
            ps.setNegative(entryBillSon.getNegative() + "");
            ps.setNum(entryBillSon.getNum());
            ps.setLiweight(entryBillSon.getLiweight() + "");
            ps.setLitotalweight(entryBillSon.getLitotalweight());
            ps.setPrice(entryBillSon.getPrice() + "");
            ps.setModel(entryBillSon.getModel());
            ps.setLocality(entryBillSon.getLocality());
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            if (Utils.isNotEmpty(byName)) {
                byName.setCode(entryBillSon.getCode());
                byName.setNum(entryBillSon.getNum());
                byName.setLitotalweight(entryBillSon.getLitotalweight());
                productStorageBiz.updateNum(byName, loginUser);
            } else {
                productStorageBiz.save(ps, loginUser);
            }

            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(EntryBillSon entryBillSon, User loginUser) throws Exception {
        EntryBillSon b2 = entryBillSonBiz.getById(entryBillSon.getId());
        if (entryBillSonBiz.update(entryBillSon, loginUser)) {
            //主表 总金额更新
            Double total = entryBillSonBiz.calcTotalPrice(entryBillSon, loginUser);
            EntryBill entryBill = new EntryBill(entryBillSon.getEntryBillId() + "", total + "");
            entryBillBiz.updateTotal(entryBill, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(entryBillSon.getWarehousename());
            ps.setProductname(entryBillSon.getProductname());
            ps.setSpecification(entryBillSon.getSpecification());
            ps.setLengths(entryBillSon.getLengths() + "");
            ps.setNegative(entryBillSon.getNegative() + "");
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            try {
                byName.setNum(ArithUtil.sub(entryBillSon.getNum(), b2.getNum()));
                byName.setLitotalweight(ArithUtil.sub(entryBillSon.getLitotalweight(),
                        b2.getLitotalweight()));
                productStorageBiz.updateNum(byName, loginUser);
            } catch (Exception e) {
                throw new Exception("没有对应的库存数据！");
            }

            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult remove(EntryBillSon entryBillSon, User loginUser) throws Exception {
        EntryBillSon b2 = entryBillSonBiz.getById(entryBillSon.getId());
        if (entryBillSonBiz.remove(entryBillSon.getId(), loginUser)) {
            //主表 总金额更新
            Double total = entryBillSonBiz.calcTotalPrice(entryBillSon, loginUser);
            EntryBill entryBill = new EntryBill(entryBillSon.getEntryBillId() + "", total + "");
            entryBillBiz.updateTotal(entryBill, loginUser);

            //修改库存
            ProductStorage ps = new ProductStorage();
            ps.setWarehousename(b2.getWarehousename());
            ps.setProductname(b2.getProductname());
            ps.setSpecification(b2.getSpecification());
            ps.setLengths(b2.getLengths() + "");
            ps.setNegative(b2.getNegative() + "");
            ProductStorage byName = productStorageBiz.getByName(ps, loginUser);
            try {
                byName.setNum(ArithUtil.sub(0, b2.getNum()));
                byName.setLitotalweight(ArithUtil.sub(0,
                        b2.getLitotalweight()));
                productStorageBiz.updateNum(byName, loginUser);
            } catch (Exception e) {
                throw new Exception("没有对应的库存数据！");
            }
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
